package dbms.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dbms.record.Schema;

public class ResultSet{
	//save data according to the column
	public Map<String, List<Object>> resultSet = new HashMap<String, List<Object>>();
	
	public List<String> fieldList = new ArrayList<String>();
	
	public int resultNumber = 0;
	
	public String resultName;
	
	public ResultSet (Map<String, List<Object>> resultSet) {
		this.resultSet = resultSet;
		for (String str : resultSet.keySet()) {
			this.fieldList.add(str);
		}
	}
	
	public ResultSet (String resultName, Map<String, List<Object>> resultSet) {
		this.resultName = resultName;
		this.resultSet = resultSet;
		for (String str : resultSet.keySet()) {
			this.fieldList.add(str);
		}
	}
	
	public ResultSet (Schema schema, Map<Integer, List<Object>> resultMap) {
		this.resultNumber = resultMap.size();
		Map<String, Integer> rank = schema.getFieldRank();
		int n = rank.size();
		String[] index = new String[n];
		for (String attr : rank.keySet()) {
			index[rank.get(attr)] = attr;
		}
		for (int i = 0; i < n; i++) {
			fieldList.add(index[i]);
		}
		
		int column = 0;
		for (String attr : rank.keySet()) {
			List<Object> temp = new ArrayList<Object>();
			for (Integer j : resultMap.keySet()) {
				temp.add(resultMap.get(j).get(column));
			}
			column++;
			resultSet.put(attr, temp);
		}
	}
	
	public String print() {
		String result = "";
		String str = "";
		for (String attr : resultSet.keySet()) {
			result.concat(attr + " ");
			str = attr;
		}
		int line = resultSet.get(str).size();
		result.concat("#");
		for (int i = 0; i < line; i++) {
			for (String attr : resultSet.keySet()) {
				result.concat(resultSet.get(attr).get(i) + " ");
			}
			result.concat("#");
		}
		return result;
	}/*
	
	public void print() {
		System.out.println("start printing result set");
		//Debug.P(schema.getInfo().keySet());
		int n = resultSet.size();
		for (int i = 1; i <= n; i++) {
			//Debug.P(i);
			List<Object> temp = resultSet.get(i);
			for (Object object : temp) {
				System.out.print(object + "|");
			}
			System.out.println();
		}
	}*/
}
